if (navigator.userAgent.match("iPhone")) {
        var audio1 = new Audio();
        audio1.id = "audio1";
}
var gAudioContext = new webkitAudioContext();
function playAmr(obj, url,onLoading,onPlay,onPlayEnd) {
    console.log("playAmr");
    console.log(url);
    console.log(obj);
    if (navigator.userAgent.match("iPhone")) {
        obj.ontouchstart = function(){
            playIos(url);
            onLoading()
        };
        obj.ontouchend = function(){
            audio1.play();
            audio1.onplay=onPlay;
            audio1.onended=onPlayEnd;
        };
        console.log("iphone");
    }
    else {
        obj.onclick = function(){
            playAndroid(url);
            onLoading()
            console.log(obj);
        };
        console.log("android");
    }
    function playAndroid(url) {
        fetchBlob(url, function (blob) {
            playAmrBlob(blob);
            console.log(url)
        });
    }
    function playIos(url) {
        fetchBlob(url, function (blob) {
            convertAmrBlobToWav(blob);
        });
    }
    function getAudioContext() {
        if (!gAudioContext) {
            gAudioContext = new AudioContext();
        }
        return gAudioContext;
    }
    function fetchBlob(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = 'blob';
        xhr.onload = function () {
            callback(this.response);
        };
        xhr.onerror = function () {
            alert('Failed to fetch ' + url);
        };
        xhr.send();
    }
    function readBlob(blob, callback) {
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = new Uint8Array(e.target.result);
            callback(data);
        };
        reader.readAsArrayBuffer(blob);
    }
    function fetchAndReadBlob(url, callback) {
        fetchBlob(url, function (blob) {
            readBlob(blob, callback);
        });
    }
    function playAmrBlob(blob, callback) {
        readBlob(blob, function (data) {
            playAmrArray(data);
        });
    }
    function convertAudioBlobToAmr(blob) {
        readBlob(blob, function (data) {
            var ctx = getAudioContext();
            ctx.decodeAudioData(data.buffer, function (audioBuffer) {
                var pcm;
                if (audioBuffer.copyFromChannel) {
                    pcm = new Float32Array(audioBuffer.length);
                    audioBuffer.copyFromChannel(pcm, 0, 0);
                } else {
                    pcm = audioBuffer.getChannelData(0);
                }
                var amr = AMR.encode(pcm, audioBuffer.sampleRate, 7);
                playAmrArray(amr);
            });
        });
    }
    function playAmrArray(array) {
        var samples = AMR.decode(array);
        if (!samples) {
            alert('Failed to decode!');
            return;
        }
        playPcm(samples);
    }
    function playPcm(samples) {
        var ctx = getAudioContext();
        var src = ctx.createBufferSource();
        var buffer = ctx.createBuffer(1, samples.length, 8000);
        if (buffer.copyToChannel) {
            buffer.copyToChannel(samples, 0, 0)
        } else {
            var channelBuffer = buffer.getChannelData(0);
            channelBuffer.set(samples);
        }
        src.buffer = buffer;
        src.connect(ctx.destination);
        src.start();
        // console.log(samples.length);
        setTimeout(onPlayEnd,samples.length/8000*1000);
        onPlay();
    }
    function convertAmrBlobToWav(blob) {
        readBlob(blob, function (data) {
            var buffer = AMR.toWAV(data);
            var url = URL.createObjectURL(new Blob([buffer], { type: 'audio/x-wav' }));
            audio1.src = url;
        });
    }
}